﻿using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.PB;
using Coldairarrow.IBusiness.DTO;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Coldairarrow.Business.PB
{
    public partial class PB_LineProcecureBusiness : BaseBusiness<PB_LineProcecure>, IPB_LineProcecureBusiness, ITransientDependency
    {
        public PB_LineProcecureBusiness(IDbAccessor db)
            : base(db)
        {
        }   

        #region 外部接口

        public async Task<PageResult<PB_LineProcecure>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<PB_LineProcecure>();
            var search = input.Search;

            //筛选
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<PB_LineProcecure, bool>(
                    ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
                where = where.And(newWhere);
            }

            return await q.Where(where).GetPageResultAsync(input);
        }

        public async Task<PB_LineProcecure> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task AddDataAsync(PB_LineProcecure data)
        {
            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(PB_LineProcecure data)
        {
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        public async Task DeleteOtherIdDataAsync(object where)
        {
            await DeleteOtherIdAsync(where);
        }

        public async Task<PageResult<PB_LineProcecure_PB_ProcecureDto>> GetDataListByAsync(PageInput<ConditionDTO> input)
        {
            var search = input.Search;
            Expression<Func<PB_LineProcecure, PB_Procecure, PB_LineProcecure_PB_ProcecureDto>> select = (a, b) => new PB_LineProcecure_PB_ProcecureDto
            {
                LineId = b.DeptId
               
            };
            select = select.BuildExtendSelectExpre();
            var q = from a in GetIQueryable().AsExpandable()
                    join b in Db.GetIQueryable<PB_Procecure>() on a.ProcecureId equals b.Id into ab
                    from b in ab.DefaultIfEmpty()
                    join c in Db.GetIQueryable<PB_Procecure>() on a.ProcecureId equals c.Id into ac
                    from c in ac.DefaultIfEmpty()
                    join d in Db.GetIQueryable<PB_Procecure>() on a.LineId equals d.Id into ad
                    from d in ad.DefaultIfEmpty()
                    select @select.Invoke(a, b);
            if (!search.Keyword.IsNullOrEmpty())
            {
                var keyword = $"%{search.Keyword}%";
                q = q.Where(x =>
                      EF.Functions.Like(x.LineId, keyword)
                      || EF.Functions.Like(x.LineId, keyword));
            }
            return await q.GetPageResultAsync(new PageInput());
        }

        public async Task<List<PB_LineProcecureSelect>> GetDataListAsync(string LineId)
        {
           
            Expression<Func<PB_LineProcecure, PB_Line, PB_ProcecureMain, PB_LineProcecureSelect>> select = (a, b, c) => new PB_LineProcecureSelect
            {
                Code = c.Code,
                Name = c.Name,
             
            };
            select = select.BuildExtendSelectExpre();
            var q = from a in GetIQueryable().AsExpandable()
                    join b in Db.GetIQueryable<PB_Line>() on a.LineId equals b.Id into ab
                    from b in ab.DefaultIfEmpty()
                    join c in Db.GetIQueryable<PB_ProcecureMain>() on a.ProcecureId equals c.Id into ac
                    from c in ac.DefaultIfEmpty()
                    select @select.Invoke(a, b, c);
            //筛选
            if (!LineId.IsNullOrEmpty())
            {
                q = q.Where(w => w.LineId == LineId);
            }
            var res = await q.ToListAsync();

            return res;
        }

        #endregion

        #region 私有成员

        #endregion
    }
}